<?php

/**
 * Parameter actions.
 *
 * @package    ProdAssistant
 * @subpackage Parameter
 * @author     Paweł Świtalla
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class ParameterActions extends sfActions
{
  public function executeCreate(sfWebRequest $request)
  {
    $this->form = new ParametersForm();
    $core = PACore::getInstance();
    $url = $request->getReferer();
    if($request->isMethod('post')){
      $this->form->bind($request->getParameter('parameters'));
      if($this->form->isValid())
      {
        $parameter = $this->form->save();
        if($request->isXmlHttpRequest())
        {
          $this->getResponse()->setContent('location@'.$url."#");
          return sfView::NONE;
        }
        else $this->redirect($core->generateAppUrl('Core', 'storeSettings'));
      }
    }
    if($request->isXmlHttpRequest())
    {
      $errors = '';
      foreach($this->form->getFormFieldSchema() as $field){
        if($field->hasError()){
          $errors .= ucfirst($field->getName()).': '.$field->getError()."\n";
        }
      }
      $this->getResponse()->setContent('error@'.$errors."#");
      return sfView::NONE;
    }
    else $this->referer = $url;
  }
  
  public function executeUpdate(sfWebRequest $request)
  {
    $id = $request->getParameter('id');
    $param = Doctrine_Core::getTable('Parameters')->find($id);
    $this->form = new Parameter($param);
  }
  
  public function executeDelete(sfWebRequest $request)
  {
    $id = $request->getParameter('id');
    $param = Doctrine_Core::getTable('Parameters')->find($id);
    $delete = $param->delete();
    $this->redirectIf($delete, $request->getReferer());
  }
}
